package com.cndw.kungfu.model.activity.holiday;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.LoverDayRankingVO;

public class LoverDayData implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -731232316680063222L;
	
	/**
	 * 送花玩家集合
	 */
	private transient Map<Long, Integer> senderMap = new ConcurrentHashMap<Long, Integer>();
	/**
	 * 收花玩家集合
	 */
	private transient Map<Long, Integer> receiverMap = new ConcurrentHashMap<Long, Integer>();
	
	private transient Map<Integer, LoverDayRankingVO> senderRanking = new HashMap<Integer, LoverDayRankingVO>();
	private transient Map<Integer, LoverDayRankingVO> receiverRanking = new HashMap<Integer, LoverDayRankingVO>();
	
	private static transient ConcurrentLinkedQueue<LoverDayLog> receiverLogs = new ConcurrentLinkedQueue<LoverDayLog>();
	
	private static int lastUpdateTime;
	
	private static int size = 10;
	
	public static final int endTime;
	
	static {
		int tmpEnd = 0;
		try {
			tmpEnd = TimeProcesser.getUnixTimeByString("2012-08-28 00:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		endTime = tmpEnd;
	}
	
	
	
	public int getEndtime() {
		return endTime;
	}
	
	public void alterSenderMap(long playerId, int num) {
		if (!senderMap.containsKey(playerId)) {
			senderMap.put(playerId, num);
		} else {
			senderMap.put(playerId, senderMap.get(playerId) + num);
		}
	}
	
	public void alterReceiverMap(long playerId, int num) {
		if (!receiverMap.containsKey(playerId)) {
			receiverMap.put(playerId, num);
		} else {
			receiverMap.put(playerId, receiverMap.get(playerId) + num);
		}
	}
	
	public int getSendNum(long playerId) {
		if (!senderMap.containsKey(playerId)) {
			return 0;
		}
		return senderMap.get(playerId);
	}
	
	public int getReceiveNum(long playerId) {
		if (!receiverMap.containsKey(playerId)) {
			return 0;
		}
		return receiverMap.get(playerId);
	}
	
	private void rank() {
		if (TimeProcesser.getUnixTime() >= endTime) {
			return;
		}
		
		if (senderRanking == null) {
			senderRanking = new HashMap<Integer, LoverDayRankingVO>();
		} else {
			senderRanking.clear();
		}
		if (receiverRanking == null) {
			receiverRanking = new HashMap<Integer, LoverDayRankingVO>();
		} else {
			receiverRanking.clear();
		}
		
		List<Entry<Long, Integer>> sendRankList = new ArrayList<Map.Entry<Long,Integer>>(senderMap.entrySet());
		Collections.sort(sendRankList, new Comparator<Entry<Long, Integer>>(){
			@Override
			public int compare(Entry<Long, Integer> o1, Entry<Long, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		});

		for (int order = 0; order < sendRankList.size(); order++) {
			Entry<Long, Integer> entry = sendRankList.get(order);
			LoverDayRankingVO e = new LoverDayRankingVO();
			Player p = Context.getPlayerServer().getByPlayerId(entry.getKey());
			e.setFlowerNum(entry.getValue());
			e.setName(p.getPlayerName());
			e.setPlayerId(entry.getKey());
			e.setRanking(order + 1);
			e.setvId(p.getVocationId());
			senderRanking.put(order + 1, e);
			if (senderRanking.size() >= size) {
				break;
			}
		}
		
		List<Entry<Long, Integer>> receiverRankList = new ArrayList<Entry<Long,Integer>>(receiverMap.entrySet());
		Collections.sort(receiverRankList, new Comparator<Entry<Long, Integer>>(){
			@Override
			public int compare(Entry<Long, Integer> o1, Entry<Long, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		});
		for (int order = 0; order < receiverRankList.size(); order++) {
			Entry<Long, Integer> entry = receiverRankList.get(order);
			LoverDayRankingVO e = new LoverDayRankingVO();
			Player p = Context.getPlayerServer().getByPlayerId(entry.getKey());
			e.setFlowerNum(entry.getValue());
			e.setName(p.getPlayerName());
			e.setPlayerId(entry.getKey());
			e.setRanking(order + 1);
			e.setvId(p.getVocationId());
			receiverRanking.put(order + 1, e);
			if (receiverRanking.size() >= size) {
				break;
			}
		}
		lastUpdateTime = TimeProcesser.getUnixTime();
		if (TimeProcesser.getUnixTime() >= getEndtime()) {
			Context.getGeneralServer().addTitle(senderRanking.get(1).getPlayerId(), 61);//发放情圣称号
			Context.getGeneralServer().addTitle(receiverRanking.get(1).getPlayerId(), 62);//发放万人迷称号
		}
	}
	
	public Map<Integer, LoverDayRankingVO> getSenderRanking() {
		if (TimeProcesser.getUnixTime() - lastUpdateTime > 300) {
			synchronized (this) {
				if (TimeProcesser.getUnixTime() - lastUpdateTime > 300) {
					rank();
				}
			}
		}
		return senderRanking;
	}
	
	public Map<Integer, LoverDayRankingVO> getReceiverRanking() {
		if (TimeProcesser.getUnixTime() - lastUpdateTime > 300) {
			synchronized (this) {
				if (TimeProcesser.getUnixTime() - lastUpdateTime > 300) {
					rank();
				}
			}
		}
		return receiverRanking;
	}
	
	public void sendsAdd(long source,long target,int type){
		LoverDayLog send = new LoverDayLog();
		send.setS(source);
		send.setT(target);
		send.setType(type);
		send.setOk(true);
		receiverLogs.add(send);
	}
	
	public boolean sendsChk(long sendId, long revId, int type) {
		
		Iterator<LoverDayLog> a = receiverLogs.iterator();
		while (a.hasNext()) {
			LoverDayLog flowerSend = (LoverDayLog) a.next();
			if (flowerSend.getS() == sendId && flowerSend.getT() == revId &&  flowerSend.getType() == type && flowerSend.isOk() ) {
				flowerSend.setOk(false);
				a.remove();
				return true;
			}			
		}
		
		return false;
	}

}
